package com.tea.service.impl;

import com.tea.service.FileStorageService;
import com.tea.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * OSS文件存储服务实现
 * @author tea-shop
 */
@Slf4j
@Service
@ConditionalOnProperty(prefix = "aliyun.oss", name = "enabled", havingValue = "true")
public class OssFileStorageServiceImpl implements FileStorageService {
    
    @Resource
    private OssUtil ossUtil;
    
    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        log.info("使用OSS存储上传文件: {}", file.getOriginalFilename());
        return ossUtil.uploadFile(file);
    }
    
    @Override
    public String uploadFile(MultipartFile file, String directory) throws IOException {
        log.info("使用OSS存储上传文件到目录 {}: {}", directory, file.getOriginalFilename());
        return ossUtil.uploadFile(file, directory);
    }
    
    @Override
    public String uploadBytes(byte[] bytes, String fileName, String contentType) {
        log.info("使用OSS存储上传字节数组: {}", fileName);
        return ossUtil.uploadBytes(bytes, fileName, contentType);
    }
    
    @Override
    public boolean deleteFile(String fileName) {
        log.info("使用OSS存储删除文件: {}", fileName);
        return ossUtil.deleteFile(fileName);
    }
    
    @Override
    public boolean deleteFileByUrl(String fileUrl) {
        log.info("使用OSS存储删除文件（URL）: {}", fileUrl);
        return ossUtil.deleteFileByUrl(fileUrl);
    }
    
    @Override
    public boolean doesFileExist(String fileName) {
        return ossUtil.doesFileExist(fileName);
    }
    
    @Override
    public String getStorageType() {
        return "OSS";
    }
}